package com.own.business.sync.trigger.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.mapper.SyncTriggerMapper;
import com.own.business.sync.trigger.base.property.SyncTriggerProperty;
import com.own.business.sync.trigger.base.service.SyncTriggerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * SyncTriggerServiceImpl
 *
 * @author chenxueli
 * @date 2024-01-08 15:39:55
 */
@Slf4j
@Service
public class SyncTriggerServiceImpl implements SyncTriggerService {

    private static LambdaQueryWrapper<SyncTrigger> OLDEST_DATA_WRAPPER;
    private static LambdaQueryWrapper<SyncTrigger> COUNT_WRAPPER;
    private static LambdaQueryWrapper<SyncTrigger> LIST_LIMIT_WRAPPER;

    @Resource
    private SyncTriggerProperty syncTriggerProperty;

    @Resource
    private SyncTriggerMapper syncTriggerMapper;

    @PostConstruct
    private void init() {
        // 最近的数据
        OLDEST_DATA_WRAPPER = new LambdaQueryWrapper<SyncTrigger>()
                .eq(SyncTrigger::getAppKey, syncTriggerProperty.getAppKey())
                .orderByAsc(SyncTrigger::getId, SyncTrigger::getGmtCreate)
                .last("limit 1");
        // 数量
        COUNT_WRAPPER = new LambdaQueryWrapper<SyncTrigger>()
                .eq(SyncTrigger::getAppKey, syncTriggerProperty.getAppKey());
        // 前n条
        LIST_LIMIT_WRAPPER = new LambdaQueryWrapper<SyncTrigger>()
                .notIn(syncTriggerProperty.getWithoutModuleNameList() != null && !syncTriggerProperty.getWithoutModuleNameList().isEmpty(), SyncTrigger::getModuleName, syncTriggerProperty.getWithoutModuleNameList())
                .eq(SyncTrigger::getAppKey, syncTriggerProperty.getAppKey())
                .last("limit " + syncTriggerProperty.getStoreLimit());
    }

    /**
     * 查询数量信息
     *
     * @return 数量
     */
    @Override
    public long count() {
        return syncTriggerMapper.selectCount(COUNT_WRAPPER);
    }

    /**
     * 获取最旧的一条数据
     *
     * @return 最旧的一条数据
     */
    @Override
    public SyncTrigger getOldestData() {
        return syncTriggerMapper.selectOne(OLDEST_DATA_WRAPPER);
    }

    /**
     * 根据id删除信息
     *
     * @param id 主键id
     */
    @Override
    public void deleteById(Long id) {
        syncTriggerMapper.deleteById(id);
    }

    /**
     * 根据ids删除信息
     *
     * @param idList 主键id
     */
    @Override
    public void deleteByIds(Collection<Long> idList) {
        if (idList.isEmpty()) {
            return;
        }
        syncTriggerMapper.deleteBatchIds(idList);
    }

    /**
     * 查询前n条数据
     *
     * @return 数据
     */
    @Override
    public List<SyncTrigger> listForLimit() {
        return syncTriggerMapper.selectList(LIST_LIMIT_WRAPPER);
    }

}
